#include <iostream>

using std::cout;
using std::endl;

template <typename T>
class Singleton
{
public:
    template <typename ...Args>//模板参数包
    static T *getInstance(Args ...args)//函数参数包
    {
        if(nullptr == _pInstance)
        {
            _pInstance = new T(args...);
            _ar;//实例化
        }
        return _pInstance;
    }

private:
    class AutoRelease
    {
    public:
        AutoRelease()
        {
            cout << "AutoRelease()" << endl;
        }

        ~AutoRelease()
        {
            cout << "~AutoRelease()" << endl;
            if(_pInstance)
            {
                delete _pInstance;
                _pInstance = nullptr;
            }
        }
    };
private:
    static T *_pInstance;
    static AutoRelease _ar;
};

template <typename T>
T *Singleton<T>::_pInstance = nullptr;

template <typename T>
class Singleton<T>::AutoRelease Singleton<T>::_ar;

class Point
{
public:
    Point(int ix = 0, int iy = 0)
    : _ix(ix)
    , _iy(iy)
    {
        cout << "Point(int = 0, int  = 0)" << endl;
    }

    void print() const
    {
        cout << "(" << _ix
             << ", " << _iy
             << ")" << endl;
    }

    ~Point()
    {
        cout << "~Point()" << endl;
    }
private:
    int _ix;
    int _iy;
};

void test()
{
    Point *pt4 = Singleton<Point>::getInstance(3, 4);
    Point *pt3 = Singleton<Point>::getInstance(1, 2);
    pt3->print();
    pt4->print();
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

